package com.demo.delivery.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.demo.delivery.entity.User;

public class CurrentUserUtils {

    private static final String CURRENT_USER = "CURRENT_USER";

    private final static SharedPreferences sp = AppUtils.getApplication().getSharedPreferences(CURRENT_USER, Context.MODE_PRIVATE);

    public static User getCurrentUser() {
        String username = sp.getString("username", null);
        Integer id = sp.getInt("id", 0);
        if (username == null) {
            return null;
        }
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        return user;
    }

    public static void setCurrentUser(User user) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", user.getUsername());
        editor.putInt("id", user.getId());
        editor.apply();
    }
}
